package com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.biz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.evaluation.common.biz.service.EvaluationWorkFlowService;
import com.paic.mhis.hcpms.evaluation.common.constants.EvaluationConstants;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dao.EvaluationOrgDao;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvalMarkDeatilInfoDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvalMarkInfoDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvalMarkUserComfirmDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.EvaluationMarkContentDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.evaluationIntegrity.dto.IntegrityEvalDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.biz.service.OrgReviewService;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.constants.OrgReviewConstants;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dao.OrgApproveDao;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dao.OrgReviewDao;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dto.OrgEvaluationOpinionDTO;
import com.paic.mhis.hcpms.evaluation.evaluationOrg.orgReview.dto.OrgReviewComfirmDTO;

/**
 * 
 * @author EX-LUORONG001
 * 
 */
@Component("orgReviewServiceImpl")
public class OrgReviewServiceImpl implements OrgReviewService {
	@Autowired
	private OrgReviewDao orgReviewDao;
	@Autowired
	private EvaluationOrgDao evaluationOrgDao;
	@Autowired
	private OrgApproveDao orgApproveDao;
	@Autowired
	private EvaluationWorkFlowService workFlowService;

	public Map<String, Object> getIntgrityEvalTypeInfo(IntegrityEvalDTO bean) {
		Map<String, Object> map = new HashMap<String, Object>();
		// 二、根据诚信考评id及考评类别：机构，药店；获取诚信考评各分组类详细信息
		List<OrgReviewComfirmDTO> cUserComfirmList = orgReviewDao.getEvalMarkUserComfirmInfo(bean.getId());
		map.put("intgrityEvalTypeInfo", cUserComfirmList);
		return map;
	}

	public Map<String, Object> getIntgrityEvalDetail(String ruleId, String evalChildId) {
		Map<String, Object> map = new HashMap<String, Object>();

		EvalMarkUserComfirmDTO evalMarkUserDTO = new EvalMarkUserComfirmDTO();
		List<EvaluationMarkContentDTO> contentList = this.orgReviewDao.queryEvaluationContentDTO(ruleId);

		List<EvaluationMarkContentDTO> markContentList = getContentList(contentList, evalChildId);
		evalMarkUserDTO.setList(markContentList); // 设置规则下面的考核内容
		map.put("data", evalMarkUserDTO);
		return map;
	}

	private List<EvaluationMarkContentDTO> getContentList(List<EvaluationMarkContentDTO> contentList, String evalChildId) {
		List<EvaluationMarkContentDTO> markContentList = new ArrayList<EvaluationMarkContentDTO>();
		if (contentList != null && contentList.size() > 0) {
			for (EvaluationMarkContentDTO contentBean : contentList) {

				String contentId = contentBean.getId();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("evalChildId", evalChildId);
				map.put("evaluationInfoId", contentId);
				List<EvalMarkDeatilInfoDTO> markRuleList = this.orgReviewDao.queryEvaluationMarkRuleDTO(map);
				if (null != markRuleList && markRuleList.size() > 0) {
					List<EvalMarkDeatilInfoDTO> evalMarkDeatilInfoList = this
							.getMarkRuleList(markRuleList, evalChildId);
					contentBean.setList(evalMarkDeatilInfoList); // 设置考核内容下面的扣分细则
					markContentList.add(contentBean);
				}

			}
		}
		return markContentList;
	}

	private List<EvalMarkDeatilInfoDTO> getMarkRuleList(List<EvalMarkDeatilInfoDTO> markRuleList, String evalChildId) {
		List<EvalMarkDeatilInfoDTO> evalMarkDeatilInfoList = new ArrayList<EvalMarkDeatilInfoDTO>();
		if (markRuleList != null && markRuleList.size() > 0) {
			for (EvalMarkDeatilInfoDTO markRuleBean : markRuleList) {
				String markRuleId = markRuleBean.getId();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("evalChildId", evalChildId);
				map.put("markRuleId", markRuleId);
				List<EvalMarkInfoDTO> markValueList = this.orgReviewDao.queryEvaluationMarkValueDTO(map);
				if (null != markValueList && markValueList.size() > 0) {
					markRuleBean.setList(markValueList); // 设置扣分细则下面的扣分值
					evalMarkDeatilInfoList.add(markRuleBean);
				} else {
					continue;
				}
				BigDecimal upperValue = markRuleBean.getUpperValue();
				BigDecimal lowerValue = markRuleBean.getLowerValue();
				if (upperValue != null && lowerValue != null) {
					markRuleBean.setMarkContent(markRuleBean.getMarkContent() + " " + lowerValue + "-" + upperValue
							+ "分");
				} else if (upperValue != null) {
					markRuleBean.setMarkContent(markRuleBean.getMarkContent() + " " + upperValue + "分");
				} else if (lowerValue != null) {
					markRuleBean.setMarkContent(markRuleBean.getMarkContent() + " " + lowerValue + "分");
				}
			}
		}
		return evalMarkDeatilInfoList;
	}

	/**
	 * 审核
	 */
	@Transactional
	public Map<String, Object> processAddOpinion(OrgReviewComfirmDTO confirmDTO, UserDetails user,
			Map<String, Object> map) throws Exception {

		OrgEvaluationOpinionDTO paramterOpinoin = new OrgEvaluationOpinionDTO();
		paramterOpinoin.setIntgritEvalChildId(confirmDTO.getIntgrityEvalChildId());
		paramterOpinoin.setTaskStatus(OrgReviewConstants.eval_status_3);

		List<OrgEvaluationOpinionDTO> list = this.orgApproveDao.getEvaluationOpinionInfoByIsValid(paramterOpinoin);

		if (null != list && list.size() > 0) {
			for (OrgEvaluationOpinionDTO updateBean : list) {
				updateBean.setOpertionUser(user.getUsername());
				updateBean.setOpinion(confirmDTO.getOpinion());
				updateBean.setOpinionDesc(confirmDTO.getOpinionDesc());
				updateBean.setUpdateUser(user.getUsername());
				updateBean.setIsValid(OrgReviewConstants.is_valid_0);
				this.orgApproveDao.updateOrgEvaluationOpinionDTO(updateBean);

			}
		} else {

			OrgEvaluationOpinionDTO evaOpinionDTO = new OrgEvaluationOpinionDTO();
			evaOpinionDTO.setIntgritEvalChildId(confirmDTO.getIntgrityEvalChildId());
			evaOpinionDTO.setOpertionUser(user.getUsername());
			evaOpinionDTO.setOpinion(confirmDTO.getOpinion());
			evaOpinionDTO.setOpinionDesc(confirmDTO.getOpinionDesc());
			evaOpinionDTO.setTaskStatus(OrgReviewConstants.eval_status_3);
			evaOpinionDTO.setCreateUser(user.getUsername());
			evaOpinionDTO.setUpdateUser(user.getUsername());
			evaOpinionDTO.setIsValid(OrgReviewConstants.is_valid_0);

			this.orgApproveDao.insertOrgEvaluationOpinionDTO(evaOpinionDTO);

		}

		long evalCount = this.orgApproveDao.getEvalInfoRecordById(confirmDTO.getEvaluationCode());
		long opinionCountDisagree = this.orgApproveDao.getEvaluationOpinionRecord(confirmDTO.getEvaluationCode(),
				OrgReviewConstants.opinion_0, OrgReviewConstants.eval_status_3, OrgReviewConstants.is_valid_0);

		long opinionCountAgress = this.orgApproveDao.getEvaluationOpinionRecord(confirmDTO.getEvaluationCode(),
				OrgReviewConstants.opinion_1, OrgReviewConstants.eval_status_3, OrgReviewConstants.is_valid_0);
		
		//if (evalCount == opinionCountDisagree) { // 不同意
		if(opinionCountDisagree>0&&opinionCountAgress+opinionCountDisagree==evalCount){
			String status = EvaluationConstants.STATUSTWO;
			evaluationOrgDao.updateIntgrityEvalStatus(status, user.getUsername(), confirmDTO.getEvaluationCode());
			// 开启流程
			Map<String, String> condition = new HashMap<String, String>();
			condition.put(EvaluationConstants.EVALUATION_REVIEW_TYPE, EvaluationConstants.EVALUATION_REVIEW_TYPE_01);
			workFlowService.processWorkFlow(confirmDTO.getEvaluationCode(), user, condition,
					EvaluationConstants.MODULE_TYPE_05);
			//重置考评保存信息,使全部考评详细变为待考评
			HashMap para=new HashMap<Object,Object>();
			para.put("updateBy", user.getUsername());
			para.put("evaluationCode",confirmDTO.getEvaluationCode());
			orgReviewDao.reSetEvalOpinion(para);
			map.put("success", OrgReviewConstants.FLAG_TURE);
		}

		if (evalCount == opinionCountAgress) { // 同意
			String status = EvaluationConstants.STATUSFIVE;
			evaluationOrgDao.updateIntgrityEvalStatus(status, user.getUsername(), confirmDTO.getEvaluationCode());
			// 开启流程
			Map<String, String> condition = new HashMap<String, String>();
			condition.put(EvaluationConstants.EVALUATION_REVIEW_TYPE, EvaluationConstants.EVALUATION_REVIEW_TYPE_02);
			workFlowService.processWorkFlow(confirmDTO.getEvaluationCode(), user, condition,
					EvaluationConstants.MODULE_TYPE_05);

			map.put("success", OrgReviewConstants.FLAG_TURE);
		}

		map.put("flag", OrgReviewConstants.FLAG_TURE);
		map.put(OrgReviewConstants.MSG_KEY, OrgReviewConstants.OPERATE_SUCCESS);
		return map;
	}

	@Transactional
	@Override
	public Map<String, Object> batchSumit(String auditOpinion, String auditDesc, List<String> list, UserDetails user)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		for (String evaluationCode : list) {
			List<EvalMarkUserComfirmDTO> markUserDTOs = evaluationOrgDao.getEvalMarkUserComfirmInfo(evaluationCode);
			// 同意
			if (OrgReviewConstants.opinion_1.equals(auditOpinion)) {
				evaluationOrgDao.updateIntgrityEvalStatus(EvaluationConstants.STATUSFIVE, user.getUsername(),
						evaluationCode);
				// 开启流程
				Map<String, String> condition = new HashMap<String, String>();
				condition
						.put(EvaluationConstants.EVALUATION_REVIEW_TYPE, EvaluationConstants.EVALUATION_REVIEW_TYPE_02);
				workFlowService.processWorkFlow(evaluationCode, user, condition, EvaluationConstants.MODULE_TYPE_05);
				this.reviewOpinion(markUserDTOs, auditOpinion, auditDesc, user.getUsername());
			} else { // 不同意
				evaluationOrgDao.updateIntgrityEvalStatus(EvaluationConstants.STATUSTWO, user.getUsername(),evaluationCode);
				// 开启流程
				Map<String, String> condition = new HashMap<String, String>();
				condition.put(EvaluationConstants.EVALUATION_REVIEW_TYPE, EvaluationConstants.EVALUATION_REVIEW_TYPE_01);
				workFlowService.processWorkFlow(evaluationCode, user, condition, EvaluationConstants.MODULE_TYPE_05);
				this.reviewOpinion(markUserDTOs, auditOpinion, auditDesc, user.getUsername());
				//重置考评保存信息
				HashMap para=new HashMap<Object,Object>();
				//para.put("createdBy",user.getUsername());
				para.put("updateBy", user.getUsername());
				para.put("evaluationCode",evaluationCode);
				orgReviewDao.reSetEvalOpinion(para);
			}
		}
		map.put(OrgReviewConstants.MSG_KEY, "操作成功!");
		map.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.SUCCESS_STATE_VALUE);
		return map;
	}

	// 审核意见
	private void reviewOpinion(List<EvalMarkUserComfirmDTO> markUserDTOs, String auditOpinion, String auditDesc,
			String userName) {
		for (EvalMarkUserComfirmDTO markUserDTO : markUserDTOs) {
			OrgEvaluationOpinionDTO opinionDto = new OrgEvaluationOpinionDTO();
			opinionDto.setTaskStatus(OrgReviewConstants.eval_status_3);
			opinionDto.setIntgritEvalChildId(markUserDTO.getIntgrityEvalChildId());
			// 判断是否撤回
			if (OrgReviewConstants.opinion_1.equals(auditOpinion)) {
				opinionDto.setIsRevoke(OrgReviewConstants.IS_REVOKE_0);
			} else {
				opinionDto.setIsRevoke(OrgReviewConstants.IS_REVOKE_1);
			}
			List<OrgEvaluationOpinionDTO> evaluationOpinionDTOs = orgApproveDao
					.getEvaluationOpinionInfoByIsValid(opinionDto);
			// 判断 如果存在就更新批量的意见,不存在就新增意见
			if (null != evaluationOpinionDTOs && evaluationOpinionDTOs.size() > 0) {
				for (OrgEvaluationOpinionDTO bean : evaluationOpinionDTOs) {
					bean.setOpinion(auditOpinion);
					bean.setOpertionUser(userName);
					bean.setOpinionDesc(auditDesc);
					bean.setIsValid(OrgReviewConstants.is_valid_0);
					this.orgApproveDao.updateOrgEvaluationOpinionDTO(bean);
				}
			} else {
				opinionDto.setOpinion(auditOpinion);
				opinionDto.setOpinionDesc(auditDesc);
				opinionDto.setOpertionUser(userName);
				opinionDto.setIsValid(OrgReviewConstants.is_valid_0);
				opinionDto.setCreateUser(userName);
				opinionDto.setUpdateUser(userName);
				this.orgApproveDao.insertOrgEvaluationOpinionDTO(opinionDto);
			}
		}
	}

	/**
	 * 诚信分析岗对诚信审核的撤销
	 */
	@Override
	public boolean cancelMedialIntegrity(Map<String, Object> map) throws Exception {
		boolean flag = true;
		UserDetails user = (UserDetails) map.get("user");
		String evaluationCode = (String) map.get("evaluationCode");
		map.put("updatedBy", user.getUsername());
		map.put("evaluationCode", evaluationCode);
		map.put("status", OrgReviewConstants.eval_status_3);
		int count = orgApproveDao.opinionCount(map);
		if (count > 0) {
			flag = false;
		} else {
			map.put("status", OrgReviewConstants.eval_status_1);
			// 更新状态
			this.orgApproveDao.cancelMedialIntegrityApprove(map);
			// 撤销流程
			this.workFlowService.processWorkFlow(evaluationCode, user, user.getUsername(), null,
					EvaluationConstants.MODULE_TYPE_05, "N2015502");
		}
		return flag;
	}

	/**
	 * 审核岗位被审批岗位退回来后，审核岗位重新提交操作
	 */
	@Transactional
	@Override
	public Map<String, Object> auditBackSaveEvaluationOpinion(OrgReviewComfirmDTO confirmDTO, UserDetails user,
			Map<String, Object> map) throws Exception {

		OrgEvaluationOpinionDTO evaOpinionDTO = new OrgEvaluationOpinionDTO();
		evaOpinionDTO.setIntgritEvalChildId(confirmDTO.getIntgrityEvalChildId());
		evaOpinionDTO.setTaskStatus(OrgReviewConstants.eval_status_3);

		List<OrgEvaluationOpinionDTO> list = this.orgApproveDao.getEvaluationOpinionInfoByIsValid(evaOpinionDTO);

		if (null != list && list.size() > 0) {
			OrgEvaluationOpinionDTO updateBean = list.get(0);

			updateBean.setOpertionUser(user.getUsername());
			updateBean.setUpdateUser(user.getUsername());
			updateBean.setOpinion(confirmDTO.getOpinion());
			updateBean.setOpinionDesc(confirmDTO.getOpinionDesc());
			updateBean.setIsValid(OrgReviewConstants.is_valid_0);

			this.orgApproveDao.updateOrgEvaluationOpinionDTO(updateBean);
			map.put(OrgReviewConstants.STATE_KEY, OrgReviewConstants.FLAG_TURE);
		}

		long evalCount = this.orgApproveDao.getEvalInfoRecordById(confirmDTO.getEvaluationCode());
		long opinionCountDisagree = this.orgApproveDao.getEvaluationOpinionRecord(confirmDTO.getEvaluationCode(),
				OrgReviewConstants.opinion_0, OrgReviewConstants.eval_status_3, OrgReviewConstants.is_valid_0);

		long opinionCountAgress = this.orgApproveDao.getEvaluationOpinionRecord(confirmDTO.getEvaluationCode(),
				OrgReviewConstants.opinion_1, OrgReviewConstants.eval_status_3, OrgReviewConstants.is_valid_0);

		if (evalCount == opinionCountDisagree) { // 不同意
			String status = EvaluationConstants.STATUSTWO;
			evaluationOrgDao.updateIntgrityEvalStatus(status, user.getUsername(), confirmDTO.getEvaluationCode());

			// 开启流程
			Map<String, String> condition = new HashMap<String, String>();
			condition.put(EvaluationConstants.EVALUATION_REVIEW_TYPE, EvaluationConstants.EVALUATION_REVIEW_TYPE_01);
			workFlowService.processWorkFlow(confirmDTO.getEvaluationCode(), user, condition,
					EvaluationConstants.MODULE_TYPE_05);

			this.orgApproveDao.reSetEvalOpinion(confirmDTO.getEvaluationCode(), OrgReviewConstants.eval_status_3,
					OrgReviewConstants.is_valid_1);
			map.put("success", OrgReviewConstants.FLAG_TURE);

		}

		if (evalCount == opinionCountAgress) { // 同意
			String status = EvaluationConstants.STATUSFIVE;
			evaluationOrgDao.updateIntgrityEvalStatus(status, user.getUsername(), confirmDTO.getEvaluationCode());
			// 开启流程
			Map<String, String> condition = new HashMap<String, String>();
			condition.put(EvaluationConstants.EVALUATION_REVIEW_TYPE, EvaluationConstants.EVALUATION_REVIEW_TYPE_02);
			workFlowService.processWorkFlow(confirmDTO.getEvaluationCode(), user, condition,
					EvaluationConstants.MODULE_TYPE_05);
			map.put("success", OrgReviewConstants.FLAG_TURE);
		}
		map.put(OrgReviewConstants.MSG_KEY, OrgReviewConstants.OPERATE_SUCCESS);
		return map;
	}
}
